namespace SimpleWebServices.Web.UI.Html.Elements
{
    using System;
    using System.Xml.Serialization;
    using SimpleWebServices;
    using Xunit;

    public sealed class ImageFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<Image>()
                .DerivesFrom<Element>()
                .IsConcreteClass()
                .IsUnsealed()
                .HasDefaultConstructor()
                .Implements<IAttributeAlternativeText>()
                .Implements<IXmlSerializable>()
                .XmlRoot("img")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            Image expected = new Image
            {
                Id = "id",
                AlternativeText = "alternative",
                Class = "class",
                Height = 100,
                Language = "fr",
                LongDescription = new Uri("http://example.com/longdesc"),
                Source = new Uri("http://example.com/source"),
                Title = "title",
                Width = 200
            };

            string xml = "<img id='id' class='class' lang='fr' title='title' alt='alternative' height='100' longdesc='http://example.com/longdesc' src='http://example.com/source' width='200' />";
            Image actual = xml.Deserialize<Image>();

            Assert.Equal<string>(expected.ToString(), actual.ToString());
        }

        [Fact]
        public void deserializeEmpty()
        {
            Assert.NotNull("<img />".Deserialize<Image>());
        }

        [Fact]
        public void serialize()
        {
            Image obj = new Image
            {
                Id = "id",
                AlternativeText = "alternative",
                Class = "class",
                Height = 100,
                Language = "fr",
                LongDescription = new Uri("http://example.com/longdesc"),
                Source = new Uri("http://example.com/source"),
                Title = "title",
                Width = 200
            };

            string xpath = "1=count(/img[@id='id'][@class='class'][@lang='fr'][@title='title'][@alt='alternative'][@height='100'][@longdesc='http://example.com/longdesc'][@src='http://example.com/source'][@width='200'])";

            Assert.True((bool)obj.Serialize().CreateNavigator().Evaluate(xpath));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new Image());
        }

        [Fact]
        public void ctor_Uri_int_int_string()
        {
            Uri source = new Uri("http://example.com/image.jpg");

            Image obj = new Image(source, 10, 20, "Alt text");

            Assert.Same(source, obj.Source);
            Assert.Equal<int>(10, obj.Height.Value);
            Assert.Equal<int>(20, obj.Width.Value);
            Assert.Equal<string>("Alt text", obj.AlternativeText);
        }

        [Fact]
        public void prop_AlternativeText()
        {
            Assert.True(new PropertyExpectations(typeof(Image).GetProperty("AlternativeText"))
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void prop_Height()
        {
            Assert.True(new PropertyExpectations(typeof(Image).GetProperty("Height"))
                .IsAutoProperty<int?>()
                .Result);
        }

        [Fact]
        public void prop_LongDescription()
        {
            Assert.True(new PropertyExpectations(typeof(Image).GetProperty("LongDescription"))
                .IsAutoProperty<Uri>()
                .Result);
        }

        [Fact]
        public void prop_Source()
        {
            Assert.True(new PropertyExpectations(typeof(Image).GetProperty("Source"))
                .IsAutoProperty<Uri>()
                .Result);
        }

        [Fact]
        public void prop_Title()
        {
            Assert.True(new PropertyExpectations(typeof(Image).GetProperty("Title"))
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void prop_Width()
        {
            Assert.True(new PropertyExpectations(typeof(Image).GetProperty("Width"))
                .IsAutoProperty<int?>()
                .Result);
        }

        [Fact]
        public void op_Deserialize_string_string_string()
        {
            Image expected = new Image
            {
                Id = "id",
                AlternativeText = "alternative",
                Class = "class",
                Height = 100,
                Language = "fr",
                LongDescription = new Uri("http://example.com/longdesc"),
                Source = new Uri("http://example.com/source"),
                Title = "title",
                Width = 200
            };

            string xml = "<img id='id' class='class' lang='fr' title='title' alt='alternative' height='100' longdesc='http://example.com/longdesc' src='http://example.com/source' width='200' />";
            Image actual = new Image().Deserialize("img", null, xml) as Image;

            Assert.Equal<string>(expected.ToString(), actual.ToString());
        }
    }
}